//
//  LearningTableView.m
//  HotWord
//
//  Created by Ji Qi on 12-6-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LearningTableView.h"
#import "LearningCell.h"
#import "Meaning.h"
#import "MeaningHeaderView.h"
#import "LearningStretchableCell.h"

@implementation LearningTableView

@synthesize tableView;
@synthesize index;
@synthesize word;
@synthesize wordLabel;
@synthesize slugLabel;


#pragma updateandReset

-(void)awakeFromNib
{
    [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, 130, 0)];
}

- (void)updateWord:(Word *)_word{
    self.word = _word;
    wordLabel.text = self.word.content;
    slugLabel.text = self.word.slug;
    if (slugLabel.text == nil) {
        slugLabel.text = @"暂无音标";
    }
    [self.tableView reloadData];
    
}


- (void)reset{
    [wordLabel removeAllSubviews];
    [slugLabel removeAllSubviews];
    [self.tableView setContentOffset:CGPointZero];
    self.word = nil;
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.word.meanings count];   
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    Meaning *meaning = [self.word.meanings objectAtIndex:section];
    NSInteger cellCount = [meaning.sentences count] + [meaning.antonyms count] + [meaning.synonyms count];
    return cellCount;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 140;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    Meaning *meaning = [word.meanings objectAtIndex:indexPath.section];
    NSInteger row = indexPath.row;
    NSInteger sentCount = [meaning.sentences count];
    //NSInteger synCount = [meaning.synonyms count];
    //NSInteger antCount = [meaning.antonyms count];
    
    if(row <= sentCount -1){
        NSString *sentence = [meaning.sentences objectAtIndex:row];
        CGSize stringSize = [sentence sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(180, 500) lineBreakMode:UILineBreakModeWordWrap];
        return stringSize.height + 30;
    }
    else{
        return 38;
    }

}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    Meaning *meaning = [self.word.meanings objectAtIndex:section];
    MeaningHeaderView *header = [[[NSBundle mainBundle] loadNibNamed:@"MeaningHeaderView" owner:self options:nil] objectAtIndex:0];
    [header setChineseTrans:meaning.chineseTranslate andEnglish:meaning.englishTranslate];
    [header.englishLabel.text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(180, 500) lineBreakMode:UILineBreakModeWordWrap];
    int i = section + 1;
    int ti = [self.word.meanings count];
    [header setIndexLabel:i atTotal:ti];
    
    return  header;
}



- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *CellIdentifier = @"LearningCellIdentifier";
    static NSString *StretchableCellIdentifier = @"LearningStretchableCellIdentifier";
    

    
    Meaning *meaning = [word.meanings objectAtIndex:indexPath.section];
    NSInteger row = indexPath.row;
    NSInteger sentCount = [meaning.sentences count];
    NSInteger synCount = [meaning.synonyms count];
    
    
    if(row <= sentCount - 1){
        NSString *sentence = [meaning.sentences objectAtIndex:indexPath.row];

        LearningStretchableCell *stretchableCell = (LearningStretchableCell *)[_tableView dequeueReusableCellWithIdentifier:StretchableCellIdentifier];
            if (stretchableCell == nil) {
                stretchableCell = [[[NSBundle mainBundle] loadNibNamed:@"LearningStretchableCell" owner:self options:nil] objectAtIndex:0];
            }
            
        [stretchableCell setContentForCell:sentence];
            
        return stretchableCell;
    
    
    }else if(row <= synCount + sentCount - 1){
        NSString *synonyms = [meaning.synonyms objectAtIndex:indexPath.row - sentCount];
        
       LearningCell * cell = (LearningCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"LearningCell" owner:self options:nil] objectAtIndex:0];
        }
        
        cell.textLabel.text = synonyms;
        cell.category.text = @"S";
        return cell;
        
    }else{
        NSString *antonyms = [meaning.antonyms objectAtIndex:indexPath.row - sentCount - synCount];
        
        LearningCell *cell = (LearningCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"LearningCell" owner:self options:nil] objectAtIndex:0];
        }
        
        cell.textLabel.text = antonyms;
        cell.category.text = @"A";
        return cell;
    }

}




- (void)dealloc{
    [word release];
    [tableView release];
    [wordLabel release];
    [slugLabel release];
    [super dealloc];
}








//default
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
